package com.yt.app.h5.controller.app;

import com.yt.app.h5.controller.base.BaseAction;
import com.yt.app.h5.exception.AppException;
import com.yt.order.api.interfaces.InstitutionService;
import com.yt.order.api.interfaces.ProductService;
import com.yt.order.api.model.Institution;
import com.yt.order.api.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by dashan on 11/21/17.
 */
@Controller
@RequestMapping("/app/product")
public class AppProductController extends BaseAction {

    @Autowired
    private ProductService productService;

    @Autowired
    private InstitutionService institutionService;

    @RequestMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "id", required = true, defaultValue = "0") Long id) {
        ModelAndView mav = new ModelAndView("/product/detail");
        if (id != null && id > 0) {
            Product product = productService.get(id);
            if(product!=null) {
                Institution institution=institutionService.get(product.getIdInstitution());
                if(institution!=null){
                    product.setInstitutionName(institution.getName());
                }
                mav.addObject("bean", product);
            }
        } else {
            throw AppException.newInstance("商品ID不能为空！");
        }
        return mav;
    }

}
